Skip to content

Fix using setFilters() does not trigger "afterUpdateRuleFilter"#353

Merged
mistic100 merged 1 commit intomistic100:masterfrom
VanTanev:fix-set-filters-not-trigering-update
Sep 9, 2016
Merged

Fix using setFilters() does not trigger "afterUpdateRuleFilter"#353
mistic100 merged 1 commit intomistic100:masterfrom
VanTanev:fix-set-filters-not-trigering-update

Conversation

@VanTanev
Copy link
Contributor

@VanTanev VanTanev commented Sep 8, 2016

Because of the way DOM elements are updated in place with setFilters,
we need to manually trigger the event or it will be omitted.

Sorry, but I don't know how to write a test for this.

Because of the way DOM elements are updated in place with setFilters,
we need to manually trigger the event or it will be omitted.
@VanTanev VanTanev changed the title Fix using setRules() does not trigger "afterUpdateRuleFilter" Fix using setFilters() does not trigger "afterUpdateRuleFilter" Sep 8, 2016
@mistic100
Copy link
Owner

The rule filter did not changed, so it makes no sense to trigger "afterUpdateRuleFilter".

If you have special logic to apply to the DOM, then listen to "afterSetFilters" too.

@VanTanev
Copy link
Contributor Author

VanTanev commented Sep 8, 2016

Hey, when I opened the PR yesterday it was at the end of a very tiring workday. I probably wasn't as descriptive as I could've been, so let me elaborate.

We need to trigger afterUpdateRuleFilter there because https://github.com/VanTanev/jQuery-QueryBuilder/blob/96f2d228eb3a6b6b8e23eaa0179edcd5f4d96b5a/src/plugins/change-filters/plugin.js#L53 creates a <select> and triggers afterCreateRuleFilters but does not set its value.

In my use case, I listen to ``afterCreateRuleFiltersand attachChosen` on the select. The chosen will not have any value set.

Afterwards, you manually call val() on the original select, but I have no way to know that the value has been updated, and my Chosen is left without selection.

That's why we need to trigger the afterUpdateRuleFilter event - because line 55 does exactly that, it updates the value of the <select>.

Then, in my plugin I can do:

    this.on('afterUpdateRuleFilter', function(e, rule) {
        rule.$el.find(QueryBuilder.selectors.rule_filter).trigger('chosen:updated');
    });

An alternative approach could be to change the template for filterSelect from:

<option value="{{= filter.id }}">{{= it.translate(filter.label) }}</option>

to:

<option value="{{= filter.id }}"
  {{? it.rule.filter && it.rule.filter.id && filter.id === it.rule.filter.id }}
    selected="selected"
  {{?}}
>{{= it.translate(filter.label) }}</option>

I should note that this problem arises both when manually using setFilters() and when you pass a rules object to the constructor of the plugin.

@mistic100
Copy link
Owner

mistic100 commented Sep 8, 2016

Yeah I understood the first time, but my point is that events triggered by the plugin represent changes in the data model, and in this case the data model did not changed, only the DOM.

What you want is exactly what the plugin his doing itself for 'bt-selectpicker' plugin

this.$el.find(Selectors.rule_filter).selectpicker('render');

Hence my suggestion to also listen to 'afterSetFilters' and refresh all Chosen at once.

@VanTanev
Copy link
Contributor Author

VanTanev commented Sep 8, 2016

I do believe that either of my proposed changes are a cleaner path to achieve this behavior.

If the data did not change, then we probably want to render the DOM in the right state immediately by updating the template to set the selected property.

@mistic100
Copy link
Owner

Right. Actually my assertion is false because events like "afterCreateRuleFilters" do not reflect a change in the model.

I'll see tomorrow.

@VanTanev
Copy link
Contributor Author

VanTanev commented Sep 8, 2016

Thank you.

@mistic100 mistic100 merged commit 7aa0640 into mistic100:master Sep 9, 2016
@mistic100
Copy link
Owner

About rules constructor : #342

@mistic100 mistic100 added this to the 2.3.4 milestone Sep 11, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments